package edu.towson.cosc.cosc455.jyoun.project1;

import java.util.ArrayList;

import edu.towson.cosc.cosc455.interfaces.SyntaxAnalyzer;

public class MySyntaxAnalyzer implements SyntaxAnalyzer {
	MyLexicalAnalyzer lexer = new MyLexicalAnalyzer();
	public static ArrayList<String> parsetree = new ArrayList<String>();
	
	
	@Override
	public void document() throws CompilerException {
		
		if(!MyCompiler.token.equalsIgnoreCase("#Start Document")){
			String message = "A #Start Document annotation was expected when a" + MyCompiler.token +" was found!";
			
				System.out.println(message);
				System.exit(0);
		}
		parsetree.add(MyCompiler.token);
		lexer.getNextToken();
		body();
		head();
		define();
		
		if(!MyCompiler.token.equalsIgnoreCase("#End Document")){
			String message = "A #End Document annotation was expected when a" + MyCompiler.token +" was found!";
			System.out.println(message);
			System.exit(0);
		}
		else{
			parsetree.add(MyCompiler.token);
			}

	}
	public void body() throws CompilerException {
		// TODO Auto-generated method stub
		//lexer.getNextToken();
		if(MyCompiler.token.equalsIgnoreCase("#Start Paragraph")){
			lexer.getNextToken();
			paragraph();
			body();
		}
		else if(MyCompiler.token.equalsIgnoreCase("#Start Bold")){
			lexer.getNextToken();
			bold();
			body();
		}
		else if(MyCompiler.token.equalsIgnoreCase("#Start Italics")){
			lexer.getNextToken();
			italics();
			body();
		}
		else if(MyCompiler.token.equalsIgnoreCase("#Start List")){
			lexer.getNextToken();
			list();
			body();
		}
		else if(MyCompiler.token.equalsIgnoreCase("#Newline")){
			parsetree.add(MyCompiler.token);
			newline();
			body();
		}
		else{
			innertext();
			body();
		}
	}
	
	@Override
	public void head() throws CompilerException {
		// TODO Auto-generated method stub
		if(!MyCompiler.token.equalsIgnoreCase("#Start Head")){
			String message = "A #Start Head annotation was expected when a" + MyCompiler.token +" was found!";
			 System.out.println(message);
             System.exit(0);
		}
		else{
		lexer.getNextToken();
		parsetree.add(MyCompiler.token);
		}
		title();
		
		if(!MyCompiler.token.equalsIgnoreCase("#End Head")){
			String message = "A #End Head annotation was expected when a" + MyCompiler.token +" was found!";
			 System.out.println(message);
             System.exit(0);
		
			
		}
		else{
			parsetree.add(MyCompiler.token);
			}
	}

	@Override
	public void title() throws CompilerException {
		// TODO Auto-generated method stub
		if(!MyCompiler.token.equalsIgnoreCase("#Start Title")){
			String message = "A #Start Title annotation was expected when a" + MyCompiler.token +" was found!";
			System.out.println(message);
            System.exit(0);
		}
		
		else{
			parsetree.add(MyCompiler.token);
			lexer.getNextToken();
            }
		innertext();
		
		if(!MyCompiler.token.equalsIgnoreCase("#End Title")){
			String message = "A #End Title annotation was expected when a" + MyCompiler.token +" was found!";
			System.out.println(message);
            System.exit(0);
		}
		else{
			parsetree.add(MyCompiler.token);
			}
	}
	

	@Override
	public void paragraph() throws CompilerException {
		// TODO Auto-generated method stub
		if(!MyCompiler.token.equalsIgnoreCase("#Start Paragraph")){
			String message = "A #Start Paragraph annotation was expected when a" + MyCompiler.token +" was found!";
			System.out.println(message);
            System.exit(0);
		} 
         else{
        	 parsetree.add(MyCompiler.token);
        	 lexer.getNextToken();
             }
		define();
		innerparagraph();
	
		
		if(!MyCompiler.token.equalsIgnoreCase("#End Paragraph")){
			String message = "A #End Paragraph annotation was expected when a" + MyCompiler.token +" was found!";
			System.out.println(message);
            System.exit(0);
		}
		else{
			parsetree.add(MyCompiler.token);
			}
		
	}

	
	@Override
	public void bold() throws CompilerException {
		// TODO Auto-generated method stub
		if(!MyCompiler.token.equalsIgnoreCase("#Start Bold")){
			String message = "A #Start Bold annotation was expected when a" + MyCompiler.token +" was found!";
			 System.out.println(message);
             System.exit(0);
		}
		else{
			parsetree.add(MyCompiler.token);
            lexer.getNextToken();
		}
		define();
		innertext();
		
		if(!MyCompiler.token.equalsIgnoreCase("#End Bold")){
			String message = "A #End Bold annotation was expected when a" + MyCompiler.token +" was found!";
			 System.out.println(message);
             System.exit(0);
		}
		else{
		parsetree.add(MyCompiler.token);
		}
	}

	@Override
	public void italics() throws CompilerException {
		// TODO Auto-generated method stub
		if(!MyCompiler.token.equalsIgnoreCase("#Start Italics")){
			String message = "A #Start Italics annotation was expected when a" + MyCompiler.token +" was found!";
			System.out.println(message);
            System.exit(0);
    }
    else{
    	parsetree.add(MyCompiler.token);
    	lexer.getNextToken();
    	}
		define();
		innertext();
		
		if(!MyCompiler.token.equalsIgnoreCase("#End Italics")){
			String message = "A #End Italics annotation was expected when a" + MyCompiler.token +" was found!";
			System.out.println(message);
            System.exit(0);
            }
		else{
			parsetree.add(MyCompiler.token);
			lexer.getNextToken();
			}
		

	}

	@Override
	public void list() throws CompilerException {
		// TODO Auto-generated method stub
		if(!MyCompiler.token.equalsIgnoreCase("#Start List")){
			String message = "A #Start List annotation was expected when a" + MyCompiler.token +" was found!";
			System.out.println(message);
            System.exit(0);
            }
		else{
			parsetree.add(MyCompiler.token);
            lexer.getNextToken();
            }
		item();
		
		if(!MyCompiler.token.equalsIgnoreCase("#End List")){
			String message = "A #End List annotation was expected when a" + MyCompiler.token +" was found!";
			System.out.println(message);
            System.exit(0);
            }
		else{
			parsetree.add(MyCompiler.token);
			lexer.getNextToken();
			}
  
	}

	@Override
	public void item() throws CompilerException {
		// TODO Auto-generated method stub
		if(!MyCompiler.token.equalsIgnoreCase("#Start Item")){
			String message = "A #Start Item annotation was expected when a" + MyCompiler.token +" was found!";
			  System.out.println(message);
              System.exit(0);
      }
      else{
    	  parsetree.add(MyCompiler.token);
    	  lexer.getNextToken();
    	  }
		
		define();
		innerlist();
		
		if(!MyCompiler.token.equalsIgnoreCase("#End Item")){
			String message = "A #End Item annotation was expected when a" + MyCompiler.token +" was found!";
			System.out.println(message);
            System.exit(0);
		}
		else{
			parsetree.add(MyCompiler.token);
			
			}
		lexer.getNextToken();
		item();
		
	}
	


	@Override
	public void audio() throws CompilerException {
		// TODO Auto-generated method stub
		if(!MyCompiler.token.equalsIgnoreCase("#Start Audio")){
			String message = "A #Start Audio annotation was expected when a" + MyCompiler.token +" was found!";
			System.out.println(message);
            System.exit(0);
		}
		else{
			parsetree.add(MyCompiler.token);
            lexer.getNextToken();
		}
		
		innertext();
		
		if(!MyCompiler.token.equalsIgnoreCase("#End Audio")){
			String message = "A #End Audio annotation was expected when a" + MyCompiler.token +" was found!";
			System.out.println(message);
            System.exit(0);
		}
		else{
			parsetree.add(MyCompiler.token);
            lexer.getNextToken();
		}
		
	}

	@Override
	public void video() throws CompilerException {
		// TODO Auto-generated method stub
		if(!MyCompiler.token.equalsIgnoreCase("#Start Video")){
			String message = "A #Start Video annotation was expected when a" + MyCompiler.token +" was found!";
			 System.out.println(message);
             System.exit(0);
		}
		else{
			parsetree.add(MyCompiler.token);
            lexer.getNextToken();
		}
		innertext();
		
		if(!MyCompiler.token.equalsIgnoreCase("#End Video")){
			String message = "A #End Video annotation was expected when a" + MyCompiler.token +" was found!";
			System.out.println(message);
            System.exit(0);
		}
		else{
			parsetree.add(MyCompiler.token);
            lexer.getNextToken();
		}
	}

	@Override
	public void define() throws CompilerException {
		// TODO Auto-generated method stub
		if(!MyCompiler.token.equalsIgnoreCase("#Start Define")){
			String message = "A #Start Define annotation was expected when a" + MyCompiler.token +" was found!";
	        System.out.println(message);
            System.exit(0);
    }
    else{
    	parsetree.add(MyCompiler.token);
    	lexer.getNextToken();
    }
    
    if(!MyCompiler.token.equalsIgnoreCase("#Name")){
            String message = "Name annotation was expected when a " + 
                            MyCompiler.token +" was not found";
            System.out.println(message);
            System.exit(0);
    }
    else{
    	parsetree.add(MyCompiler.token);
        lexer.getNextToken();
    }
    text();

    if(!MyCompiler.token.equalsIgnoreCase("#Value")){
            String message = "Value annotation was expected when a " + 
                            MyCompiler.token +" was not found";
            System.out.println(message);
            System.exit(0);
    }
    else{
    	parsetree.add(MyCompiler.token);
            lexer.getNextToken();
    }
    text();

    if(!MyCompiler.token.equalsIgnoreCase("#End Define")){
            String message = "End Define annotation was expected when a " + 
                            MyCompiler.token +" was not found";
            System.out.println(message);
            System.exit(0);
    }
    else{
    	parsetree.add(MyCompiler.token);
        lexer.getNextToken();
        define();
    }
    
}

	@Override
	public void use() throws CompilerException {
		// TODO Auto-generated method stub
		lexer.getNextToken();
		if(!MyCompiler.token.equalsIgnoreCase("#Start Use")){
			String message = "A #Start Use annotation was expected when a " + MyCompiler.token +" was found!";
			System.out.println(message);
            System.exit(0);
    }
    else{
    	parsetree.add(MyCompiler.token);
        lexer.getNextToken();
    }
		text();
    if(!MyCompiler.token.equalsIgnoreCase("#End Use")){
            String message = "End Use annotation was expected when a " + 
                            MyCompiler.token +" was not found";
            System.out.println(message);
            System.exit(0);
    }
    else{
    	parsetree.add(MyCompiler.token);
        lexer.getNextToken();
        }
    }

	public void innerparagraph() throws CompilerException {
		// TODO Auto-generated method stub
		if(!MyCompiler.token.equalsIgnoreCase("#Start Bold")){
			lexer.getNextToken();
			bold();
			innerparagraph();
		}
		else if(!MyCompiler.token.equalsIgnoreCase("#Start Italics")){
			lexer.getNextToken();
			italics();
			innerparagraph();
		}
		else if(!MyCompiler.token.equalsIgnoreCase("#Start List")){
			lexer.getNextToken();
			list();
			innerparagraph();
		}
		else if(!MyCompiler.token.equalsIgnoreCase("#Newline")){
			lexer.getNextToken();
			newline();
			innerparagraph();
		}
		else{
			lexer.getNextToken();
			innertext();
			innerparagraph();
		}
	}
	public void text() throws CompilerException {
		if (!MyCompiler.token.startsWith("#")) {
			parsetree.add(MyCompiler.token);
			lexer.getNextToken();
		}
		
		else {
			String message = "Text  was expected when " + MyCompiler.token + " was found.";
			throw new CompilerException(message);
		}
	}
	@Override
	public void innertext() throws CompilerException {
		// TODO Auto-generated method stub
		//lexer.getNextToken();
		if(MyCompiler.token.equalsIgnoreCase("#Start Use")){
			lexer.getNextToken();
			use();
			innertext();
		}
		else if(MyCompiler.token.equalsIgnoreCase("#Newline")){
			lexer.getNextToken();
			newline();
			innertext();
		}
		else{
			lexer.getNextToken();
			innertext();
		}
	}
	@Override
	public void innerlist() throws CompilerException {
		// TODO Auto-generated method stub
		//lexer.getNextToken();
		if(MyCompiler.token.equalsIgnoreCase("#Start Bold")){
			lexer.getNextToken();
			bold();
			innerlist();
		}
		else if(MyCompiler.token.equalsIgnoreCase("#Start Italics")){
			lexer.getNextToken();
			italics();
			innerlist();
		}
		else if(MyCompiler.token.equalsIgnoreCase("#Start List")){
			lexer.getNextToken();
			list();
			innerlist();
		}
		else{
			lexer.getNextToken();
			innertext();
			innerlist();
		}
	}
	@Override
	public void newline() throws CompilerException {
		//lexer.getNextToken();
		if(!MyCompiler.token.equalsIgnoreCase("#Newline")){
            String message = "Newline annotation was expected when a " + 
                            MyCompiler.token +" was not found";
            System.out.println(message);
            System.exit(0);
    }
		else{
			parsetree.add(MyCompiler.token);
			lexer.getNextToken();
		}
	}
	

}

